package com.pzl.expandableactionbar;

import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;

/**
 * Created by Administrator on 2016-04-13.
 */
public class TestListener implements View.OnTouchListener {
    private final String TAG = "TestListener";
    float oldY, newY;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // Log.i("pzl","getScrollY="+listView.getScrollY()+"");
        // Log.i("pzl", event.getY() + "");
        // Log.i("pzl", "getMyScrollY=" + getScrollY((AbsListView) v));
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                oldY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                newY = event.getY();
                if (newY > oldY) {
                    Log.i("pzl", "↓");
                    if (isScrollTop((AbsListView) v)) {
                        Log.i("pzl", "事件被上面拦截");//只在move拦截
                    }
                } else {
                    Log.i("pzl", "↑");
                }
                oldY = newY;
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return false;
    }

    /**
     * for only one child listview
     *
     * @param listView
     * @return
     */
    public int getScrollY(AbsListView listView) {
        View c = listView.getChildAt(0);
        if (c == null) {
            return 0;
        }
        int firstVisiblePosition = listView.getFirstVisiblePosition();
        int top = c.getTop();
        return -top + firstVisiblePosition * c.getHeight();//TODO item高度不同的时候,headview,footview 的测量
    }

    /**
     * for only one child listview
     * listview是否滚动到顶部
     *
     * @return
     */
    public boolean isScrollTop(AbsListView listView) {//TODO 其它类型view
        View c = listView.getChildAt(0);
        if (c == null) {
            return true;//listview为空，使它也可以展开查看
        }
        if (c.getTop() >= 0 && listView.getFirstVisiblePosition() == 0) {
            return true;
        } else {
            return false;
        }
    }
}
